#ifndef ORDERDIALOG_H
#define ORDERDIALOG_H

#include <QDialog>
#include <qframe.h>
#include <QAbstractItemDelegate>
#include "queryresult.h"
#include "Enums.h"
#include <QPainter>
#include <QStyledItemDelegate>
#include <QPushButton>
#include "queryresultmodel.h"
#include <QVariant>
#include <QButtonGroup>
#include <QRadioButton>
#include "user.h"
namespace Ui {
class OrderDialog;
}
class SingleItemFrame:public QFrame{
    Q_OBJECT
public:
    explicit SingleItemFrame(QWidget *parent = nullptr)
        : QFrame(parent){
        setStyleSheet("border: none; background: black; border-radius: 8px;");
    }
    QSize sizeHint() const override {
        return delegate->sizeHint(QStyleOptionViewItem(), QModelIndex());
    }
    void setResult(QueryResult* res,OneWayDelegate* f){
        this->res = res;
        this->delegate = new OneWayDelegate();
        delegate->setResult(res);
        setMinimumSize(delegate->sizeHint(QStyleOptionViewItem(),QModelIndex()));
    }

    void paintEvent(QPaintEvent *event) override {
        // QPainter painter(this);
        // QStyleOptionViewItem option;
        // option.initFrom(this);
        // option.rect = rect();
        // delegate->paint(&painter, option,QModelIndex());
        QFrame::paintEvent(event); // 保留Frame背景绘制
        //if (!delegate || !currentres) return;

        QPainter painter(this);
        painter.save(); // 保存初始状态，避免与Frame绘制冲突

        QStyleOptionViewItem option;
        option.initFrom(this);
        // 关键1：确保绘制区域有效（最小100x80，避免宽高为0）
        QRect validRect = contentsRect().adjusted(0, 0, -1, -1); // 缩小1px避免超出边界
        if (validRect.width() < 100) validRect.setWidth(100);
        if (validRect.height() < 80) validRect.setHeight(80);
        option.rect = validRect;

        // 关键2：设置有效状态（启用+可交互，匹配委托按钮逻辑）
        option.state = QStyle::State_Enabled | QStyle::State_Active;
        if (hasFocus()) option.state |= QStyle::State_HasFocus;

        // 关键3：适配黑色背景的文本/按钮颜色
        option.palette.setColor(QPalette::Text, Qt::white);
        option.palette.setColor(QPalette::ButtonText, Qt::white);
        option.palette.setColor(QPalette::Highlight, Qt::gray);
        option.palette.setColor(QPalette::Button, Qt::darkGray); // 按钮背景色（避免透明）

        // 关键4：确保委托的currentResult有效（双重校验）
        //delegate->setResult(res);
        delegate->paint(&painter, option, QModelIndex());

        painter.restore(); // 恢复painter状态，避免影响其他绘制
    }

private:
    QueryResult* res;
    OneWayDelegate* delegate;
};

class OrderDialog : public QDialog
{
    Q_OBJECT

public:
    explicit OrderDialog(QueryResult*,OneWayDelegate* de,QList<User>Users,QWidget *parent = nullptr);
    ~OrderDialog();
private:
    Ui::OrderDialog *ui;
    QList<User>Users;
};

#endif // ORDERDIALOG_H
